package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.AlipayMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.AuthUserMapper;
import cc.rengu.igas.bomp.common.dao.IndustryInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AlipayMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.AuthUserMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.IndustryInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AlipayActivityMchntInfo;
import cc.rengu.igas.bomp.common.entity.AuthUser;
import cc.rengu.igas.bomp.common.entity.IndustryInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntInfoBean;
import cc.rengu.igas.bomp.facade.request.AlipayActivityApplyQueryRequest;
import cc.rengu.igas.bomp.facade.response.AlipayActivityApplyQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Updated by zhangxuran on 2020.03.23 调整修改支付宝特殊费率商户列表信息查询
 */
public class AlipayActivityApplyQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        AlipayActivityApplyQueryRequest alipayActivityApplyQueryRequest = new AlipayActivityApplyQueryRequest();
        ConvertUtil.convertOutput(alipayActivityApplyQueryRequest);
        return alipayActivityApplyQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        AlipayActivityApplyQueryRequest alipayActivityApplyQueryRequest = (AlipayActivityApplyQueryRequest) request;
        /*获取法人行号*/
        String instId = alipayActivityApplyQueryRequest.getHeader().getInstId();
        /*判断必填性校验*/
        if (StringUtil.isEmptyOrNull(alipayActivityApplyQueryRequest.getUserId()) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("用户标识不能为空");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /*获取商户号信息*/
        String mchntNo = alipayActivityApplyQueryRequest.getMchntNo();
        /*获取商户名信息*/
        String mchntName = alipayActivityApplyQueryRequest.getMchntName();
        /*获取签约机构信息*/
        String signInstId = alipayActivityApplyQueryRequest.getSignInstId();
        /*获取活动类型信息*/
        String activityType = alipayActivityApplyQueryRequest.getActivityType();
        /*特殊计费申请结果*/
        String activityApplyResult = alipayActivityApplyQueryRequest.getActivityApplyResult();
        /*当前页信息*/
        int pageIndex = alipayActivityApplyQueryRequest.getPageIndex();
        /*当前页数据条数*/
        int pageSize = alipayActivityApplyQueryRequest.getPageSize();
        /*分页信息类*/
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        /* 根据用户标识查询用户信息，获取用户组织机构 */
        AuthUserMapper authUserMapper = new AuthUserMapperImpl();
        AuthUser authUser = authUserMapper.selectAuthUserById(alipayActivityApplyQueryRequest.getUserId());
        if (null == authUser) {
            rglog.error("用户信息查询失败");
            throw new BizException(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode(), RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
        }
        /*当前登录用户机构号*/
        String orgCode = authUser.getOrgCode();
        String userInstId = authUser.getInstId();
        /*判断是否是(admin)用户*/
        if (BompAppParamConstant.BASE_ISNT_ID.equals(userInstId)) {
            /*(admin)用户,机构号取法人行总行机构号*/
            orgCode = instId + BompAppParamConstant.INSTORGCODE;
        }

        /*查询系统参数表查出符合支付宝特殊费率申请的mcc码*/
        SysParamMapper sysParamMapper = new SysParamMapperImpl();
        List<SysParam> sysParamList = sysParamMapper.selectSysParamByInstIdAndParamType(AppParamConstant.DEFAULT_INSTID, "alipayActivityImgType");
        String alipayActivityMcc = "";
        for (SysParam sysParam : sysParamList) {
            alipayActivityMcc += "," + sysParam.getParamKey();
        }
        /*关联信息查询及过滤*/
        AlipayMchntInfoMapper alipayMchntInfoMapper = new AlipayMchntInfoMapperImpl();
        PageResult pageResult = alipayMchntInfoMapper.selectAlipayActivityMchntInfoByInstid(instId, mchntNo, mchntName, signInstId, activityType, activityApplyResult, pageInfo, alipayActivityMcc, orgCode);
        if (null == pageResult) {
            throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
        }
        pageInfo = pageResult.getPageInfo();
        List<Object> objectList = pageResult.getResult();
        List<AlipayActivityMchntInfo> mchntBaseInfoList = objectList.stream().map(item -> (AlipayActivityMchntInfo) item).collect(Collectors.toList());
        IndustryInfoMapper industryInfoMapper = new IndustryInfoMapperImpl();
        /*响应信息初始化*/
        AlipayActivityApplyQueryResponse alipayActivityApplyQueryResponse = new AlipayActivityApplyQueryResponse();
        if (null != mchntBaseInfoList && !mchntBaseInfoList.isEmpty()) {
            alipayActivityApplyQueryResponse.setTotalPage(pageInfo.getTotalPage());
            alipayActivityApplyQueryResponse.setPageIndex(pageInfo.getPageIndex());
            alipayActivityApplyQueryResponse.setPageSize(pageInfo.getPageSize());
            alipayActivityApplyQueryResponse.setTotalRows(pageInfo.getTotalRows());
            List<MchntInfoBean> respMchntInfosList = new ArrayList<>();

            for (AlipayActivityMchntInfo mchntBaseInfo : mchntBaseInfoList) {
                MchntInfoBean mchntInfoBean = new MchntInfoBean();
                /* 信息复制 */
                BeanUtil.beanCopy(mchntBaseInfo, mchntInfoBean);
                /*查询行业类别名称*/
                IndustryInfo industryInfo = industryInfoMapper.selectIndustryInfoByIndustryType(mchntBaseInfo.getIndustryType());
                if (null != industryInfo) {
                    /* 行业类型名称赋值 */
                    mchntInfoBean.setIndustryName(industryInfo.getIndustryName());
                }
                respMchntInfosList.add(mchntInfoBean);
            }

            alipayActivityApplyQueryResponse.setMchntInfoList(respMchntInfosList);
        }
        /*响应信息拼装返回*/
        BizResponse<AlipayActivityApplyQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(alipayActivityApplyQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AlipayActivityApplyQueryResponse alipayActivityApplyQueryResponse = (AlipayActivityApplyQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(alipayActivityApplyQueryResponse);
    }
}